package com.shop.search.controller;

import com.shop.common.entity.CommonResult;
import com.shop.search.pojo.SearchProduct;
import com.shop.search.service.SearchProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.jws.WebParam;
import java.util.List;

/**
 * @author ghy
 * @version 1.0
 * @date 2020-11-02
 */
@Controller
@RequestMapping("/search")
public class SearchController {

    @Autowired
    SearchProductService searchProductService;

    @GetMapping("/product")
    @ResponseBody
    public List<SearchProduct> findAll(){
        return searchProductService.findAll();
    }

    /**
     * 导入数据
     * @return
     */
    @GetMapping("/import")
    @ResponseBody
    public CommonResult initData(){
        return searchProductService.initData();
    }

    @GetMapping("/do/{keyword}")
    public String search(@PathVariable(name = "keyword") String keyword, Model model){

        try {
            List list = searchProductService.search(keyword);
            model.addAttribute("products", list);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "search";
    }




}
